public class MyCircularDeque {

    int k;
    int[]  queue;
    int first;
    int end;
    public MyCircularDeque(int k) {
        first = -1;
        end = -1;
        queue = new int[k];
        this.k = k;
    }

    public boolean insertFront(int value) {
        if (isFull()) return false;
        if (isEmpty()) end = 0;
        first = (first + 1) % k;
        queue[first] = value;
        return true;
    }

    public boolean insertLast(int value) {
        if (isFull()) return false;
        if (isEmpty()) {
            first = k - 1;
            end = 0;
        }
        end = end == 0 ? k - 1 : end - 1;
        queue[end] = value;
        return true;
    }

    public boolean deleteFront() {
        if (isEmpty()) return false;
        if (first == end) {
            first = -1;
            end = -1;
        } else first = first == 0 ? k - 1 : first - 1;
        return true;
    }

    public boolean deleteLast() {
        if (isEmpty()) return false;
        if (first == end) {
            first = -1;
            end = -1;
        } else end = (end + 1) % k;
        return true;
    }

    public int getFront() {
        if (isEmpty()) return -1;
        return queue[first];
    }

    public int getRear() {
        if (isEmpty()) return -1;
        return queue[end];
    }

    public boolean isEmpty() {
        return first == -1;
    }

    public boolean isFull() {
        return (first + 1) % k == end;
    }
}
